import * as actionTypes from './actionTypes';
import { MenuAction, MenuState } from '../../types/menu';

const initGlobalState: MenuState = {
  menuList: [],
  openMenuKey: [], // 打开的菜单栏的key  用于侧边栏
  selectMenuKey: [], // 选中菜单栏的key  用户侧边栏
  openedMenu: JSON.parse(sessionStorage.getItem('tagsViewList') as any) || [], // 保存已经打开的菜单栏 用于顶部导航
  currentPath: '',
  backRouteList: JSON.parse(sessionStorage.getItem('backRouteList') as any) || [],
  isTagsViewCurrenFull: eval(sessionStorage.getItem('isLockScreen') || ''.toLowerCase()) || false, //当前页全屏
};
export default function reducer(state = initGlobalState, action: MenuAction): MenuState {
  const { type, menuItem, keys, list, path, menuItemArr, CurrenFull, backRoute } = action;
  switch (type) {
    //路由信息
    case actionTypes.SET_USERMENU: {
      return { ...state, menuList: list };
    }
    case actionTypes.SET_BACKROUTE: {
      sessionStorage.setItem('backRouteList', JSON.stringify(backRoute));
      return { ...state, backRouteList: backRoute };
    }
    //当前菜单展开得数组
    case actionTypes.SET_OPENKEY: {
      return { ...state, openMenuKey: keys };
    }
    //当前菜单选中项
    case actionTypes.SET_SELECTKEY: {
      return { ...state, selectMenuKey: keys };
    }
    //设置当前路由路径
    case actionTypes.SETCURRENTPATH: {
      return { ...state, currentPath: path };
    }
    //添加tagsview
    case actionTypes.ADDOPENTMENU: {
      if (menuItem && !state.openedMenu.find((i) => i.path === menuItem.path)) {
        const openedMenu = [...state.openedMenu];
        openedMenu.push(menuItem);
        sessionStorage.setItem('tagsViewList', JSON.stringify(openedMenu));
        return { ...state, openedMenu };
      }
      return state;
    }
    //关闭tagsview
    case actionTypes.FILTER_OPENKEY: {
      sessionStorage.setItem('tagsViewList', JSON.stringify(menuItemArr));
      return { ...state, openedMenu: menuItemArr };
    }
    //当前全屏
    case actionTypes.SETCURRENFULL: {
      sessionStorage.setItem('isLockScreen', CurrenFull.toString());
      return { ...state, isTagsViewCurrenFull: CurrenFull };
    }
    default: {
      return state;
    }
  }
}
